﻿Shader "HGUI/UIDef"
{
	Properties
	{
		_MainTex("Sprite Texture", 2D) = "white" {}
		_t1("Sprite Texture", 2D) = "white" {}
		_t2("Sprite Texture", 2D) = "white" {}
		_t3("Sprite Texture", 2D) = "white" {}
		_t4("Sprite Texture", 2D) = "white" {}
		_t5("Sprite Texture", 2D) = "white" {}
		_t6("Sprite Texture", 2D) = "white" {}
		_t7("Sprite Texture", 2D) = "white" {}
		[PerRendererData]_Color("Tint", Color) = (1,1,1,1)
		[Enum(Off,0,On,1)] _ZWrite ("ZWrite", Float) = 0
		[Enum(Less,0, Greater,1, LEqual,2, GEqual,3, Equal,4, NotEqual,5, Always,6)] _ZTest("ZTest", Float) = 6
	}

	SubShader
	{
		Tags
		{
			"Queue" = "Transparent+100"
			"IgnoreProjector" = "True"
			"RenderType" = "Transparent"
			"PreviewType" = "Plane"
			"CanUseSpriteAtlas" = "True"
		}

		Pass
		{
			//Cull Off
			Lighting Off
			ZWrite [_ZWrite]
			ZTest [_ZTest]
			//ZTest Off //[unity_GUIZTestMode]
			Blend SrcAlpha OneMinusSrcAlpha
			Name "Default"
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#pragma target 2.0

			#include "UnityCG.cginc"
			//#include "UnityUI.cginc"
			//#pragma multi_compile M_Screen M_WorldSpace
			//#pragma multi_compile __ UNITY_UI_ALPHACLIP

			struct appdata_t
			{
				float4 vertex  : POSITION;
				float4 color  : COLOR;
				float4 uv : TEXCOORD0;
				float2 uv1 : TEXCOORD1;
				float2 uv2 : TEXCOORD2;
				float2 uv3 : TEXCOORD3;
				float2 uv4 : TEXCOORD4;
				UNITY_VERTEX_INPUT_INSTANCE_ID
			};

			struct v2f
			{
				float4 vertex   : SV_POSITION;
				half4 color : COLOR;
				float4 uv  : TEXCOORD0;
				float4 uv1 : TEXCOORD1;
				float4 uv2 : TEXCOORD2;
				UNITY_VERTEX_OUTPUT_STEREO
			};

			v2f vert(appdata_t i)
			{
				v2f o= (v2f)0;
				UNITY_SETUP_INSTANCE_ID(i);
				UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
				// #ifdef M_Screen
				// 	#if UNITY_REVERSED_Z	
				// 		o.vertex.x = i.vertex.x / (_ScreenParams.x * 0.5);
				// 		o.vertex.y = -i.vertex.y / (_ScreenParams.y * 0.5);
				// 		o.vertex.z = 0.9999999;
				// 		o.vertex.w = 1;
				// 	#else
				// 		o.vertex.x = i.vertex.x / (_ScreenParams.x * 0.5);
				// 		o.vertex.y = i.vertex.y / (_ScreenParams.y * 0.5);
				// 		o.vertex.z = -1;
				// 		o.vertex.w = 1;
				// 	#endif
				// #else
					o.vertex = UnityObjectToClipPos(i.vertex);
				// #endif
				
				o.uv.xy = i.uv;
				o.uv.zw = i.uv1;
				o.uv1.xy = i.uv2;
				o.uv1.zw = i.uv3;
				o.uv2.xy = i.uv4;
				o.color = i.color;
				return o;
			}

			sampler2D _MainTex;
			sampler2D _t1;
			sampler2D _t2;
			sampler2D _t3;
			sampler2D _t4;
			sampler2D _t5;
			sampler2D _t6;
			sampler2D _t7;
			//float4 _Color;
			fixed4 frag(v2f IN) : SV_Target
			{
				half4 color;
				float2 uv = IN.uv;
				uv.x *= IN.uv2.x;
				uv.y *= IN.uv2.y;
				uv.x += IN.uv1.z;
				uv.y += IN.uv1.w;
				if (IN.uv.z < 0.09)
				{
					color = tex2D(_MainTex, uv.xy);
				}
				else if (IN.uv.z < 0.19)
				{
					color = tex2D(_t1, uv.xy);
				}
				else  if (IN.uv.z < 0.29)
				{
					color = tex2D(_t2, uv.xy);
				}
				else  if (IN.uv.z < 0.39)
				{
					color = tex2D(_t3, uv.xy);
				}
				else if (IN.uv.z < 0.49)
				{
					color = tex2D(_t4, uv.xy);
				}
				else  if (IN.uv.z < 0.59)
				{
					color = tex2D(_t5, uv.xy);
				}
				else  if (IN.uv.z < 0.69)
				{
					color = tex2D(_t6, uv.xy);
				}
				else
				{
					color = tex2D(_t7, uv.xy);
				}
				if (IN.uv.w < 0.05)
				{
					color.x *= IN.color.x;
					color.y *= IN.color.y;
					color.z *= IN.color.z;
					color.a *= IN.color.a;
				}else if (IN.uv.w < 0.09)
				{
					color = IN.color;
				}
				else if (IN.uv.w < 0.19)
				{
					color.xyz = IN.color.xyz;
					color.a *= IN.color.a;
				}
				else if (IN.uv.w < 0.29)
				{
					color.a *= IN.color.a;
				}
				else//黑白
				{
					color.rgb = dot(color.rgb, float3(0.22, 0.707, 0.071));
				}
				if (IN.uv1.x <0.499 || IN.uv1.x > 0.501|| IN.uv1.y <0.499 || IN.uv1.y > 0.501)
				color.a = 0;
				#if !UNITY_COLORSPACE_GAMMA
					else 
					{
						color.x = pow(color.x, 2.2);
						color.y = pow(color.y, 2.2);
						color.z = pow(color.z, 2.2);
					}
				#endif
				return color;
			}
			ENDCG
		}
	}
}